package main

import (
	"flag"
	"os"

	"mytest0/internal/conf"
	"github.com/go-kratos/kratos/v2"
	"github.com/go-kratos/kratos/v2/config"
	//"github.com/go-kratos/kratos/v2/config/file"
"github.com/hashicorp/consul/api"
         // Consul Key/Value configuration center
         "github.com/go-kratos/kratos/contrib/config/consul/v2"
         // Consul service registry
         consul_r "github.com/go-kratos/kratos/contrib/registry/consul/v2"
         "fmt"
         "mytest0/internal/service"
         "mytest0/internal/server"

	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/tracing"
	"github.com/go-kratos/kratos/v2/transport/grpc"
	"github.com/go-kratos/kratos/v2/transport/http"
)

// go build -ldflags "-X main.Version=x.y.z"
var (
	// Name is the name of the compiled software.
	Name string = "mytest0"
	// Version is the version of the compiled software.
	Version string = "1.0-alpha"
	// flagconf is the config flag.
	flagconf string

	 
    ConsulAddress string = "iconsul.supor.com:30058" 
    ConsulKeyValuePath string = "app/cart/configs/config.yaml"
 id, _ = os.Hostname()
)

func init() {
	 
    // Consul Key/Value(service configuration) 
    flag.StringVar (&ConsulAddress, "consul-server", "iconsul.supor.com:30058", "consul server, eg: --consul-server iconsul.supor.com:30058") 
    flag.StringVar (&ConsulKeyValuePath, "config-path", "app/cart/configs/config.yaml", "config path, eg: --config-path app/cart/configs/config.yaml") //flag.StringVar (&flagconf, "conf", "../../configs", "config path, eg: -conf config.yaml")
}

func newApp(logger log.Logger, hs *http.Server, gs *grpc.Server) *kratos.App {
	 
    consulClient, err := api.NewClient(&api.Config{ 
      Address: ConsulAddress, 
    }) 
    if err != nil { 
      fmt.Println(err) 
    } 
    r := consul_r.New(consulClient)
 return kratos.New(
		kratos.Registrar(r), //Service regist to consul.
		kratos.ID(id),
		kratos.Name(Name),
		kratos.Version(Version),
		kratos.Metadata(map[string]string{}),
		kratos.Logger(logger),
		kratos.Server(
			hs,
			gs,
		),
	)
}

func main() {
	flag.Parse()
	logger := log.With(log.NewStdLogger(os.Stdout),
		"ts", log.DefaultTimestamp,
		"caller", log.DefaultCaller,
		"service.id", id,
		"service.name", Name,
		"service.version", Version,
		"trace_id", tracing.TraceID(),
		"span_id", tracing.SpanID(),
	)
//	c := config.New(
//		config.WithSource(
//			file.NewSource(flagconf),
//		),
//	)
	 
    fmt.Printf("Consul Server -> %s\n", ConsulAddress) 
    fmt.Printf("Config Path -> %s\n", ConsulKeyValuePath) 
    consulClient, err := api.NewClient(&api.Config{ 
      Address: ConsulAddress, 
    }) 
    if err != nil { 
      panic(err) 
    } 
    cs, err := consul.New(consulClient, consul.WithPath(ConsulKeyValuePath)) 
    if err != nil { 
      panic(err) 
    } 
    // Create kratos config instance 
    var c config.Config  
    c = config.New(config.WithSource(cs))
 defer c.Close()

	if err := c.Load(); err != nil {
		panic(err)
	}

	var bc conf.Bootstrap
	if err := c.Scan(&bc); err != nil {
		panic(err)
	}

		service.MyConfigFromConsul(c)  //Load configuration-fields from consul-server to Golang-Structures.
 
        server.MyJaegerTraceProvider(c) //Load configuration-fields from consul-server to Golang-Structures, and then set jaeger trace provider 
 app, cleanup, err := initApp (bc.Server, bc.Data, logger)
	if err != nil {
		panic(err)
	}
	defer cleanup()

	// start and wait for stop signal
	if err := app.Run(); err != nil {
		panic(err)
	}
}
